/**
 * @author YinHang
 */
;(function () {
	window.Gamepackage = {
		setPath: function (path) {
			scriptPath = path;
		},
		loadClasses: function (syncUris, callback) {
			var index = 0;
			loader = function () {
				if(syncUris[index])
				{
					var curIndex = index;
					++ index;
					getScript(scriptPath + mudolePath + "/" + syncUris[curIndex].split(".").join("/") + ".js", loader);
				}
				else if(callback)
				{
					callback();	
				}
			};
			loader();
		},
		getScripts: function (syncUris, callback) {
			var index = 0;
			loader = function () {
				if(syncUris[index])
				{
					var curIndex = index;
					++ index;
					getScript(syncUris[curIndex], loader);
				}
				else if(callback)
				{
					callback();	
				}
			};
			loader();
		},
		loadImage: function (images, callback) {
			var length = images.length;
			var loaded = 0;
			function callbackTrigger() {
				if(++ loaded == length)
					if(callback)
						callback();
			};
			for(var i=0;i<length;++i)
				images[i].load(callbackTrigger);
		},
		merge: function (obj1, obj2) {
			for(var key in obj2)
				obj1[key] = obj2[key];
		},
		get: function (uri) {
			var packageNames = uri.split(".");
			var nameLength = packageNames.length;
			for(var i=0;i<nameLength;++i)
			{
				var packageUri = "";
				for(var j = 0;j<i;++j)
					packageUri += packageNames[j] + ".";
				packageUri += packageNames[i];
				if(eval(packageUri));
				else
				{
					eval(packageUri + " = {};");
				}
				if(i == nameLength - 1)
				{
					return eval(packageUri);
				}
			}
		},
		classCreator: function (classInfo) {
			
		},
		extend: function (targetClass, parent) {
			Gamepackage.merge(targetClass.prototype, new parent());
			BASECLASSES[targetClass] = parent;
			targetClass.base = BASECLASSES[targetClass];
			targetClass.prototype.constructor = targetClass;
		},
		ui: {
			g2d: {},
			g3d: {}
		}
	};
	
	var EXTEND_NUM = -1;
	var BASECLASSES = {};
	
	var g = function (id) {
		return document.getElementById(id);
	};
	
	var c = function (tagName) {
		return document.createElement(tagName);
	};
	
	var scriptPath = "javascript/Gamepackage";
	var mudolePath = "/mudole";
	
	function getScript(src, callback) {
		var scriptTag = c("script");
		scriptTag.type = "text/javascript";
		scriptTag.src = src;
		scriptTag.onload = callback;
		document.getElementsByTagName("head")[0].appendChild(scriptTag);
	};
})();
